package com.bfxy.rabbit.producer.broker;

import com.bfxy.rabbit.api.Message;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * @describte:TODO
 * @program:rabbit-parent
 * @author:zhengdp
 * @create:2021-09-16 19:24:44
 **/
public class MessageHolder {

    // 缓存 Message
    private List<Message> messages = Lists.newArrayList();

    // 存储线程中所有批量发的数据
    public static final ThreadLocal<MessageHolder> holder = new ThreadLocal<MessageHolder>(){

        @Override
        protected MessageHolder initialValue() {
            return new MessageHolder();
        }
    };

    public static void add(Message message) {
        holder.get().messages.add(message);
    }

    public static List<Message> clear(){
        // 在clear的时候将数据取出
        List<Message> temp = Lists.newArrayList(holder.get().messages);
        holder.remove();
        return temp;
    }
}
